package com.syn.synold4s.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.WatchListDto;
import com.syn.synold4s.databinding.WatchItemBinding;
import java.util.ArrayList;
import java.util.List;

public class WatchAdapter extends BaseAdapter<WatchItemBinding, WatchListDto> {

    private Integer deviceOnlineTotal;
    private Integer deviceTotal;
    private boolean isShowInfo = true;
    private boolean isMainShow = false;

    public void setDeviceOnlineTotal(Integer deviceOnlineTotal) {
        this.deviceOnlineTotal = deviceOnlineTotal;
    }

    public void setShowInfo(boolean showInfo) {
        isShowInfo = showInfo;
    }

    public void setMainShow(boolean mainShow) {
        isMainShow = mainShow;
    }

    public void setDeviceTotal(Integer deviceTotal) {
        this.deviceTotal = deviceTotal;
    }

    public WatchAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(WatchItemBinding binding, int position) {
    }

    @Override
    protected void onBindItem(WatchItemBinding binding, WatchListDto item, int position) {
        if (position == 0 && !isMainShow){
            binding.topLayout.setVisibility(View.VISIBLE);
            binding.tvAllDevice.setText(String.format(mContext.getString(R.string.all_device), deviceOnlineTotal, deviceTotal));
        }
        if (isMainShow){
            binding.tvWatchAddressS.setVisibility(View.VISIBLE);
            binding.tvWatchAddressS.setText(item.getAddress());
        } else {
            binding.tvWatchAddressS.setVisibility(View.GONE);
        }
        binding.viewWatchStatus.setBackgroundColor(mContext.getColor(item.isOnline() ? R.color.green : R.color.red));
        binding.tvWatchName.setText(item.getName() != null ? item.getName() : "");
        binding.tvWatchTime.setText(item.getDataUpdateAt() != null ? item.getDataUpdateAt() : "-- -- ----");
        binding.tvWatchHeart.setNumAndStatus(item.isShow() ? item.getHeartRate() : "--", item.isHAlert() ? R.color.red : R.color.color_text);
        binding.tvWatchTemp.setNumAndStatus(item.isShow() ? item.getTemperature() : "--", item.isTAlert() ? R.color.red : R.color.color_text);
        binding.tvAlertStatus.setText(item.getAlarmTypeName());
        binding.tvAlertTime.setText(String.format(mContext.getString(R.string.latest_alert_time), item.getAlarmTime() != null ? item.getAlarmTime() : ""));
        binding.tvImei.setText(String.format(mContext.getString(R.string.imei), item.getSerialNumber() != null ? item.getSerialNumber() : ""));
        binding.tvWatchAddress.setText(String.format(mContext.getString(R.string.address), item.getAddress() != null ? item.getAddress() : ""));

        binding.infoLayout.setVisibility(isShowInfo ? View.VISIBLE : View.GONE);
        binding.tvAllExpand.setTextColor(isShowInfo ? mContext.getColor(R.color.color_main) : mContext.getColor(R.color.color_text));
        Drawable expandDrawable = null;
        if (isShowInfo) {
            expandDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_expand);
        } else {
            expandDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_expand_close);
        }
        if (expandDrawable != null) {
            expandDrawable.setBounds(0, 0, expandDrawable.getIntrinsicWidth(), expandDrawable.getIntrinsicHeight());
        }
        binding.tvAllExpand.setCompoundDrawablesRelative(null, null, expandDrawable, null);
    }

    public void setShowOrHide(boolean isShow) {
        List<WatchListDto> currentList = getCurrentList();
        for (WatchListDto dto : currentList) {
            dto.setShow(isShow);
        }
        submitList(new ArrayList<>(currentList));
        notifyDataSetChanged();
    }

    @Override
    protected void onCreateView(WatchItemBinding binding, BaseViewHolder<WatchItemBinding> holder) {
        binding.tvAllExpand.setOnClickListener(view -> {
            isShowInfo = !isShowInfo;
            notifyDataSetChanged();
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.watch_item;
    }
}
